<template>
  <div>
    <el-table :data="allUsers" ref="roleUserTable" tooltip-effect="dark" border style="width: 100%" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="用户姓名" prop="name"></el-table-column>
    </el-table>
    <div class="mt_5 tac">
      <el-button type="primary" size="mini" @click="saveUserRole">确定</el-button>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  props:['roleId'],
  data() {
    return {
      allUsers :[],
      selectUsers:[],
    }
  },
  mounted() {
    this.loadUserRole();
  },
  methods: {
    handleSelectionChange(selectRows){
      this.selectUsers = selectRows
    },
    saveUserRole(){
      let roleUsers = [];
      this.selectUsers.forEach((r)=>{
        roleUsers.push(r.id)
      })
      request({
        url: '/sys/role/saveuserrole',
        data: {roleId : this.roleId, roleUsers:roleUsers}
      }).then(res=>{
        if(res.code === '200'){
          this.$message.success(res.msg)
        }
      })
    },
    loadUserRole(){
      request({
        url: '/sys/role/listroleuser',
        data: {roleId : this.roleId}
      }).then(res => {
        const { data } = res
        this.allUsers = data.allUsers
        this.$nextTick(()=>{
          this.allUsers.forEach((item)=>{
            if(data.roleUsers.filter((u)=> u.id === item.id).length > 0){
              this.$refs.roleUserTable && this.$refs.roleUserTable.toggleRowSelection(item, true)
            }
          })
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
